package com.ag.lzw.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbFeedbackAnnex;
import com.ag.bean.TbTaskAnnex;
import com.ag.bean.TbUri;
import com.ag.common.AnnexPath;
import com.ag.common.BaseAction;
import com.ag.lzm.utils.PlanAnnex;
import com.ag.lzm.utils.PlanUtils;
import com.ag.lzw.dao.LzwTbFeedbackAnnexDAO;
import com.ag.lzw.dao.LzwTbTaskAnnexDAO;
import com.ag.lzw.dao.LzwTbUriDAO;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author zhiweiofli 
 * 实现上传的action类
 * */
@Scope("prototype")
@Controller("uploadAction")
public class UploadAction extends BaseAction implements SessionAware {

	private static final long serialVersionUID = 7122650390836546519L;

	/**
	 * 保存任务附件的方法
	 * @return null
	 */
	public String doUpload() {
		ActionContext ac = ActionContext.getContext();
		HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println(request.getAttribute("name"));
		System.out.println(request.getParameter("name"));
		/*
		ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
		String savePath = sc.getRealPath("/");
		savePath = savePath + "uploads/";
		*/
		String savePath = null;
		TbUri url = uriDAO.findById(AnnexPath.TASK_ANNEX_PATH);
		
		if(url!=null && url.getUPath()!=null){
			savePath = url.getUPath();
		} else {
			return null;
		}
		
		File f1 = new File(savePath);
		if (!f1.exists()) {
			f1.mkdirs();
		}
		int size = Filedata.size();
		if (size == 0) {
			return ERROR;
		}
			
		String extName = null;
		String name = null;
		String path = null;
		for (int i = 0; i < size; i++) {
			extName = FiledataFileName.get(i).substring(FiledataFileName.get(i).lastIndexOf("."));
			name = UUID.randomUUID().toString();
			System.out.println("-------Annex id:"+name);
			path = savePath + name + extName;
			File file = new File(path);
			try {
				FileUtils.copyFile(Filedata.get(i), file);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		TbTaskAnnex ta = new TbTaskAnnex();
		ta.setTaId(name);
		ta.setTaAddress(path);
		taskAnnexDAO.save(ta);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			response.getWriter().print(name + extName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}
	
	/**
	 * 保存反馈附件的方法
	 * @return null
	 */
	public String uploadFeedback(){
		ActionContext ac = ActionContext.getContext();
		HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println(request.getAttribute("name"));
		System.out.println(request.getParameter("name"));
		/*
		ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
		String savePath = sc.getRealPath("/");
		savePath = savePath + "uploads/";
		*/
		String savePath = null;
		TbUri url = uriDAO.findById(AnnexPath.TASK_FEEDBACK_PATH);
		
		if(url!=null && url.getUPath()!=null){
			savePath = url.getUPath();
		} else {
			return null;
		}
		
		File f1 = new File(savePath);
		if (!f1.exists()) {
			f1.mkdirs();
		}
		int size = Filedata.size();
		if (size == 0) {
			return ERROR;
		}
			
		String extName = null;
		String name = null;
		String path = null;
		for (int i = 0; i < size; i++) {
			extName = FiledataFileName.get(i).substring(FiledataFileName.get(i).lastIndexOf("."));
			name = UUID.randomUUID().toString();
			System.out.println("-------Feedback Annex id:"+name);
			path = savePath + name + extName;
			File file = new File(path);
			try {
				FileUtils.copyFile(Filedata.get(i), file);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		TbFeedbackAnnex tfa = new TbFeedbackAnnex();
		tfa.setTaId(name);
		tfa.setTaAddress(path);
		feedbackDAO.save(tfa);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			response.getWriter().print(name + extName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 把附件文件信息放到Session里面
	 * 
	 * @param paId
	 *            文件ID，即服务器硬盘存储的值
	 * @param paName
	 *            客户原来的文件名
	 */
	private void addAnnex2Session(String paId, String paName) {
		List<PlanAnnex> paList = (List<PlanAnnex>) session
				.get(PlanUtils.PLAN_ANNEX);
		PlanAnnex pa = new PlanAnnex();
		pa.setPaId(paId);
		pa.setPaName(paName);
		if (paList == null) {
			paList = new ArrayList<PlanAnnex>();
			paList.add(pa);
		} else {
			paList.add(pa);
		}
		session.remove(PlanUtils.PLAN_ANNEX);
		session.put(PlanUtils.PLAN_ANNEX, paList);
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Resource(name="lzwTbUriDAO")
	private LzwTbUriDAO uriDAO;
	@Resource(name="lzwTbTaskAnnexDAO")
	private LzwTbTaskAnnexDAO taskAnnexDAO;
	@Resource(name="lzwTbFeedbackAnnexDAO")
	private LzwTbFeedbackAnnexDAO feedbackDAO;
	
	private Map<String, Object> session;

	private List<File> Filedata;
	private List<String> FiledataFileName;
	private List<String> FiledataContentType;
	private String name;

	public List<File> getFiledata() {
		return Filedata;
	}

	public void setFiledata(List<File> filedata) {
		Filedata = filedata;
	}

	public List<String> getFiledataFileName() {
		return FiledataFileName;
	}

	public void setFiledataFileName(List<String> filedataFileName) {
		FiledataFileName = filedataFileName;
	}

	public List<String> getFiledataContentType() {
		return FiledataContentType;
	}

	public void setFiledataContentType(List<String> filedataContentType) {
		FiledataContentType = filedataContentType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
